package com.moke.util.mouse
{
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.utils.getTimer;
	
	internal class EffectMouseBall extends Shape
	{
		static public var show:Boolean = true;
		
		private var prevTime:int = getTimer();
		private var vx:Number = Math.random() * 2 - 1;
		private var vy:Number = Math.random() * 2;
		private var scale:Number = 1.0;
		
		public function EffectMouseBall() 
		{
			initUI();
			if (stage) addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			else addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		}
		
		private function addedToStageHandler(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
		
		private function enterFrameHandler(e:Event):void 
		{
			if (!show) destroy();
			var time:int = getTimer();
			if (time - prevTime >= 50)
			{
				prevTime = time;
				this.x += vx;
				this.y += vy
				scale *= .95;
				this.scaleX = scale;
				this.scaleY = scale;
				if (width < 4.0) destroy();
			}
			
		}
		
		private function destroy():void
		{
			removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
			if (this.parent) this.parent.removeChild(this);
		}
		
		private function initUI():void
		{
			this.graphics.beginGradientFill(GradientType.RADIAL,
				[0xFFFFFF, 0xFFFFFF, 0xFFFFFF],
				[.8, .3, 0],
				[5, 11, 25]);
			this.graphics.drawCircle(0, 0, 30);
			this.graphics.endFill();
		}
	}
}